<?php 
session_start();
ob_start();
if($_GET['title']){
	$_SESSION['session_active_page'] = $_GET['title'];
}
?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php include ("ewconfig.php") ?>
<?php include ("db.php") ?>
<?php include ("bannerinfo.php") ?>
<?php include ("advsecu.php") ?>
<?php include ("phpmkrfn.php") ?>
<?php include ("ewupload.php") ?>
<?php

// Initialize common variables
$x_id = NULL;
$ox_id = NULL;
$z_id = NULL;
$ar_x_id = NULL;
$ari_x_id = NULL;
$x_idList = NULL;
$x_idChk = NULL;
$cbo_x_id_js = NULL;
$x_status = NULL;
$ox_status = NULL;
$z_status = NULL;
$ar_x_status = NULL;
$ari_x_status = NULL;
$x_statusList = NULL;
$x_statusChk = NULL;
$cbo_x_status_js = NULL;
$x_image = NULL;
$ox_image = NULL;
$z_image = NULL;
$fs_x_image = 0;
$fn_x_image = "";
$ct_x_image = "";
$wd_x_image = 0;
$ht_x_image = 0;
$a_x_image = "";
$ar_x_image = NULL;
$ari_x_image = NULL;
$x_imageList = NULL;
$x_imageChk = NULL;
$cbo_x_image_js = NULL;
$fs_x_image = 0;
$fn_x_image = "";
$ct_x_image = "";
$wd_x_image = 0;
$ht_x_image = 0;
$a_x_image = "";
$x_title = NULL;
$ox_title = NULL;
$z_title = NULL;
$ar_x_title = NULL;
$ari_x_title = NULL;
$x_titleList = NULL;
$x_titleChk = NULL;
$cbo_x_title_js = NULL;
$x_description = NULL;
$ox_description = NULL;
$z_description = NULL;
$ar_x_description = NULL;
$ari_x_description = NULL;
$x_descriptionList = NULL;
$x_descriptionChk = NULL;
$cbo_x_description_js = NULL;
$x_sort = NULL;
$ox_sort = NULL;
$z_sort = NULL;
$ar_x_sort = NULL;
$ari_x_sort = NULL;
$x_sortList = NULL;
$x_sortChk = NULL;
$cbo_x_sort_js = NULL;
$x_author_id = NULL;
$ox_author_id = NULL;
$z_author_id = NULL;
$ar_x_author_id = NULL;
$ari_x_author_id = NULL;
$x_author_idList = NULL;
$x_author_idChk = NULL;
$cbo_x_author_id_js = NULL;
$x_modified_id = NULL;
$ox_modified_id = NULL;
$z_modified_id = NULL;
$ar_x_modified_id = NULL;
$ari_x_modified_id = NULL;
$x_modified_idList = NULL;
$x_modified_idChk = NULL;
$cbo_x_modified_id_js = NULL;
$x_created_date = NULL;
$ox_created_date = NULL;
$z_created_date = NULL;
$ar_x_created_date = NULL;
$ari_x_created_date = NULL;
$x_created_dateList = NULL;
$x_created_dateChk = NULL;
$cbo_x_created_date_js = NULL;
$x_updated_date = NULL;
$ox_updated_date = NULL;
$z_updated_date = NULL;
$ar_x_updated_date = NULL;
$ari_x_updated_date = NULL;
$x_updated_dateList = NULL;
$x_updated_dateChk = NULL;
$cbo_x_updated_date_js = NULL;
?>
<?php
$nStartRec = 0;
$nStopRec = 0;
$nTotalRecs = 0;
$nRecCount = 0;
$nRecActual = 0;
$sKeyMaster = "";
$sDbWhereMaster = "";
$sSrchAdvanced = "";
$psearch = "";
$psearchtype = "";
$sDbWhereDetail = "";
$sSrchBasic = "";
$sSrchWhere = "";
$sDbWhere = "";
$sOrderBy = "";
$sSqlMaster = "";
$sListTrJs = "";
$bEditRow = "";
$nEditRowCnt = "";
$sDeleteConfirmMsg = "";
$nDisplayRecs = "20";
$nRecRange = 10;

// Set up records per page dynamically
SetUpDisplayRecs();

// Open connection to the database
$conn = phpmkr_db_connect(HOST, USER, PASS, DB, PORT);

// Handle reset command
ResetCmd();

// Build filter condition
$sDbWhere = "";
if ($sDbWhereDetail <> "") {
	if ($sDbWhere <> "") $sDbWhere .= " AND ";
	$sDbWhere .= "(" . $sDbWhereDetail . ")";
}
if ($sSrchWhere <> "") {
	if ($sDbWhere <> "") $sDbWhere .= " AND ";
	$sDbWhere .= "(" . $sSrchWhere . ")";
}

// Set up sorting order
$sOrderBy = "";
SetUpSortOrder();
$sSql = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, $sDbWhere, $sOrderBy);

// echo $sSql . "<br>"; // Uncomment to show SQL for debugging
?>
<?php include ("header.php") ?>
<script type="text/javascript">
<!--
EW_LookupFn = "ewlookup.php"; // ewlookup file name
EW_AddOptFn = "ewaddopt.php"; // ewaddopt.php file name

//-->
</script>
<script type="text/javascript" src="ewp.js"></script>
<script type="text/javascript">
<!--
EW_dateSep = "-"; // set date separator
EW_UploadAllowedFileExt = "gif,jpg,jpeg,bmp,png,swf,flv,zip,mp4"; // allowed upload file extension

//-->
</script>
<script type="text/javascript">
<!--
var firstrowoffset = 1; // first data row start at
var tablename = 'ewlistmain'; // table name
var lastrowoffset = 0; // footer row
var usecss = true; // use css
var rowclass = 'ewTableRow'; // row class
var rowaltclass = 'ewTableAltRow'; // row alternate class
var rowmoverclass = 'ewTableHighlightRow'; // row mouse over class
var rowselectedclass = 'ewTableSelectRow'; // row selected class
var roweditclass = 'ewTableEditRow'; // row edit class
var rowcolor = '#FFFFFF'; // row color
var rowaltcolor = '#FFFFFF'; // row alternate color
var rowmovercolor = '#FFFACD'; // row mouse over color
var rowselectedcolor = '#FFFACD'; // row selected color
var roweditcolor = '#7FFFD4'; // row edit color

//-->
</script>
<script type="text/javascript">
<!--
	var EW_DHTMLEditors = [];

//-->
</script>
<script type="text/javascript">
<!--
function EW_selectKey(elem) {
	var f = elem.form;	
	if (!f.elements["key_d[]"]) return;
	if (f.elements["key_d[]"][0]) {
		for (var i=0; i<f.elements["key_d[]"].length; i++)
			f.elements["key_d[]"][i].checked = elem.checked;	
	} else {
		f.elements["key_d[]"].checked = elem.checked;	
	}	
	ew_clickall(elem);
}
function EW_selected(elem) {
	var f = elem.form;	
	if (!f.elements["key_d[]"]) return false;
	if (f.elements["key_d[]"][0]) {
		for (var i=0; i<f.elements["key_d[]"].length; i++)
			if (f.elements["key_d[]"][i].checked) return true;
	} else {
		return f.elements["key_d[]"].checked;
	}
	return false;
}

//-->
</script>
<?php

// Set up recordset
$rs = phpmkr_query($sSql, $conn) or die("Failed to execute query at line " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL: ' . $sSql);
$nTotalRecs = phpmkr_num_rows($rs);
if ($nDisplayRecs <= 0) { // Display all records
	$nDisplayRecs = $nTotalRecs;
}
$nStartRec = 1;
SetUpStartRec(); // Set up start record position
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td class="admin_td_title">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
			<td class="admin_title">Banner &raquo; List &nbsp;</td>
    <td class="admin_search" align="right">
	</td>
  </tr>
</table>
</td>
  </tr>
</table>
<br />
<table class="ewListAdd">
	<tr>
		<td><span class="phpmaker"><a href="banneradd.php" class="button-modify">+ Tambah Data</a></span></td>
	</tr>
</table>
<br />
<?php
if (@$_SESSION[ewSessionMessage] <> "") {
?>
<p><span class="ewmsg"><?php echo $_SESSION[ewSessionMessage]; ?></span></p>
<?php
	$_SESSION[ewSessionMessage] = ""; // Clear message
}
?>
<?php if ($nTotalRecs > 0)  { ?>
<form method="post">
<table id="ewlistmain" class="ewTable">
	<!-- Table header -->
	<tr class="ewTableHeader">
		<td valign="top"><span>
	<a href="bannerlist.php?order=<?php echo urlencode("status"); ?>" onMouseDown="ewsort(event, this.href);">
	status<?php if (@$_SESSION[ewSessionTblSort . "_x_status"] == "ASC") { ?><img src="images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$_SESSION[ewSessionTblSort . "_x_status"] == "DESC") { ?><img src="images/sortdown.gif" width="10" height="9" border="0"><?php } ?>
	</a>
		</span></td>
		<td valign="top"><span>
	<a href="bannerlist.php?order=<?php echo urlencode("image"); ?>" onMouseDown="ewsort(event, this.href);">
	Gambar<?php if (@$_SESSION[ewSessionTblSort . "_x_image"] == "ASC") { ?><img src="images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$_SESSION[ewSessionTblSort . "_x_image"] == "DESC") { ?><img src="images/sortdown.gif" width="10" height="9" border="0"><?php } ?>
	</a>
		</span></td>
		<td valign="top"><span>
	<a href="bannerlist.php?order=<?php echo urlencode("title"); ?>" onMouseDown="ewsort(event, this.href);">
	Judul<?php if (@$_SESSION[ewSessionTblSort . "_x_title"] == "ASC") { ?><img src="images/sortup.gif" width="10" height="9" border="0"><?php } elseif (@$_SESSION[ewSessionTblSort . "_x_title"] == "DESC") { ?><img src="images/sortdown.gif" width="10" height="9" border="0"><?php } ?>
	</a>
		</span></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="checkbox" name="checkall" class="phpmaker" onClick="EW_selectKey(this);"></td>
	</tr>
<?php

// Set the last record to display
$nStopRec = $nStartRec + $nDisplayRecs - 1;

// Move to the first record
$nRecCount = $nStartRec - 1;
if (phpmkr_num_rows($rs) > 0) {
	phpmkr_data_seek($rs, $nStartRec -1);
}
$nRecActual = 0;
while (($row = @phpmkr_fetch_array($rs)) && ($nRecCount < $nStopRec)) {
	$nRecCount = $nRecCount + 1;
	if ($nRecCount >= $nStartRec) {
		$nRecActual++;

		// Set row color
		$sItemRowClass = " class=\"ewTableRow\"";
		$sListTrJs = " onmouseover='ew_mouseover(this);' onmouseout='ew_mouseout(this);' onclick='ew_click(this);'";

		// Display alternate color for rows
		if ($nRecCount % 2 <> 1) {
			$sItemRowClass = " class=\"ewTableAltRow\"";
		}
		$x_id = $row["id"];
		$x_status = $row["status"];
		$x_image = $row["image"];
		$x_title = $row["title"];
		$x_description = $row["description"];
		$x_sort = $row["sort"];
		$x_author_id = $row["author_id"];
		$x_modified_id = $row["modified_id"];
		$x_created_date = $row["created_date"];
		$x_updated_date = $row["updated_date"];
?>
	<!-- Table body -->
	<tr<?php echo $sItemRowClass; ?><?php echo $sListTrJs; ?>>
		<!-- status -->
		<td align="center"><span>
<?php
switch ($x_status) {
	case "A":
		$sTmp = "Tampil";
		break;
	case "I":
		$sTmp = "Tidak Tampil";
		break;
	default:
		$sTmp = "";
}
$ox_status = $x_status; // Backup original value
$x_status = $sTmp;
?>
<?php echo get_icon_status($ox_status); ?>
<?php $x_status = $ox_status; // Restore original value ?>
</span></td>
		<!-- image -->
		<td><span>
<?php if ((!is_null($x_image)) &&  $x_image <> "") { ?>
<img src="<?php echo ewUploadPathEx(False, "../uploads/banner/thumbs/") . $x_image ?>" border=0 width="150">
<?php } ?>
</span></td>
		<!-- title -->
		<td><span>
<?php echo $x_title; ?>
</span></td>
<td><span class="phpmaker"><a href="<?php if ($x_id <> "") {echo "bannerview.php?id=" . urlencode($x_id); } else { echo "javascript:alert('Invalid Record! Key is null');";} ?>">View</a></span></td>
<td><span class="phpmaker"><a href="<?php if ($x_id <> "") {echo "banneredit.php?id=" . urlencode($x_id); } else { echo "javascript:alert('Invalid Record! Key is null');";} ?>">Edit</a></span></td>
<td><span class="phpmaker"><input type="checkbox" name="key_d[]" value="<?php echo $x_id; ?>" class="phpmaker" onclick='ew_clickmultidelete(this);'>Delete</span></td>
	</tr>
<?php
	}
}
?>
</table>
<?php if ($nRecActual > 0) { ?>
<p>
	<input type="button" name="btndelete" value="DELETE SELECTED" onClick="if (!EW_selected(this)) alert('No records selected'); else {this.form.action='bannerdelete.php';this.form.encoding='application/x-www-form-urlencoded';this.form.submit();}">
</p>
<?php } ?>
</form>
<?php 
}

// Close recordset and connection
phpmkr_free_result($rs);
phpmkr_db_close($conn);
?>
<form action="bannerlist.php" name="ewpagerform" id="ewpagerform">
<table>
	<tr>
		<td nowrap>
		<span class="phpmaker">
<?php

// Display page numbers
if ($nTotalRecs > 0) {
	$rsEof = ($nTotalRecs < ($nStartRec + $nDisplayRecs));
	if ($nTotalRecs > $nDisplayRecs) {

		// Find out if there should be Prev/Next links
		if ($nStartRec == 1) {
			$isPrev = False;
		} else {
			$isPrev = True;
			$PrevStart = $nStartRec - $nDisplayRecs;
			if ($PrevStart < 1) { $PrevStart = 1; } ?>
		<a href="bannerlist.php?start=<?php echo $PrevStart; ?>"><b>Prev</b></a>
		<?php
		}
		if ($isPrev || (!$rsEof)) {
			$x = 1;
			$y = 1;
			$dx1 = intval(($nStartRec-1)/($nDisplayRecs*$nRecRange))*$nDisplayRecs*$nRecRange+1;
			$dy1 = intval(($nStartRec-1)/($nDisplayRecs*$nRecRange))*$nRecRange+1;
			if (($dx1+$nDisplayRecs*$nRecRange-1) > $nTotalRecs) {
				$dx2 = intval($nTotalRecs/$nDisplayRecs)*$nDisplayRecs+1;
				$dy2 = intval($nTotalRecs/$nDisplayRecs)+1;
			} else {
				$dx2 = $dx1+$nDisplayRecs*$nRecRange-1;
				$dy2 = $dy1+$nRecRange-1;
			}
			while ($x <= $nTotalRecs) {
				if (($x >= $dx1) && ($x <= $dx2)) {
					if ($nStartRec == $x) { ?>
		<b><?php echo $y; ?></b>
					<?php } else { ?>
		<a href="bannerlist.php?start=<?php echo $x; ?>"><b><?php echo $y; ?></b></a>
					<?php }
					$x += $nDisplayRecs;
					$y += 1;
				} elseif (($x >= ($dx1-$nDisplayRecs*$nRecRange)) && ($x <= ($dx2+$nDisplayRecs*$nRecRange))) {
					if ($x+$nRecRange*$nDisplayRecs < $nTotalRecs) { ?>
		<a href="bannerlist.php?start=<?php echo $x; ?>"><b><?php echo $y; ?>-<?php echo ($y+$nRecRange-1);?></b></a>
					<?php } else {
						$ny=intval(($nTotalRecs-1)/$nDisplayRecs)+1;
							if ($ny == $y) { ?>
		<a href="bannerlist.php?start=<?php echo $x; ?>"><b><?php echo $y; ?></b></a>
							<?php } else { ?>
		<a href="bannerlist.php?start=<?php echo $x; ?>"><b><?php echo $y; ?>-<?php echo $ny; ?></b></a>
							<?php }
					}
					$x += $nRecRange*$nDisplayRecs;
					$y += $nRecRange;
				} else {
					$x += $nRecRange*$nDisplayRecs;
					$y += $nRecRange;
				}
			}
		}

		// Next link
		if (!$rsEof) {
			$NextStart = $nStartRec + $nDisplayRecs;
			$isMore = True;  ?>
		<a href="bannerlist.php?start=<?php echo $NextStart; ?>"><b>Next</b></a>
		<?php } else {
			$isMore = False;
		} ?>
		<br>
<?php	}
	if ($nStartRec > $nTotalRecs) { $nStartRec = $nTotalRecs; }
	$nStopRec = $nStartRec + $nDisplayRecs - 1;
	$nRecCount = $nTotalRecs - 1;
	if ($rsEof) { $nRecCount = $nTotalRecs; }
	if ($nStopRec > $nRecCount) { $nStopRec = $nRecCount; } ?>
	Records <?php echo  $nStartRec;  ?> to <?php  echo $nStopRec; ?> of <?php echo  $nTotalRecs; ?>
<?php } else { ?>
	<?php if ($sSrchWhere == "0=101") {?>
	<?php } else { ?>
	No records found
	<?php } ?>
<?php }?>
		</span>
		</td>
<?php if ($nTotalRecs > 0) { ?>
		<td nowrap>&nbsp;&nbsp;&nbsp;&nbsp;</td>
		<td align="right" valign="top" nowrap><span class="phpmaker">Records Per Page&nbsp;
		<select name="<?php echo ewTblRecPerPage; ?>" onChange="this.form.submit();" class="phpmaker">
			<option value="10"<?php if ($nDisplayRecs == 10) { echo " selected";  }?>>10</option>
			<option value="20"<?php if ($nDisplayRecs == 20) { echo " selected";  }?>>20</option>
			<option value="50"<?php if ($nDisplayRecs == 50) { echo " selected";  }?>>50</option>
			<option value="ALL"<?php if (@$_SESSION[ewSessionTblRecPerPage] == -1) { echo " selected";  }?>>All Records</option>
		</select>
		</span></td>
<?php } ?>
	</tr>
</table>
</form>
<?php include ("footer.php") ?>
<?php

//-------------------------------------------------------------------------------
// Function SetUpDisplayRecs
// - Set up Number of Records displayed per page based on Form element RecPerPage
// - Variables setup: nDisplayRecs

function SetUpDisplayRecs()
{
	global $nStartRec;
	global $nDisplayRecs;
	global $nTotalRecs;
	$sWrk = @$_GET[ewTblRecPerPage];
	if ($sWrk <> "") {
		if (is_numeric($sWrk)) {
			$nDisplayRecs = $sWrk;
		} else {
			if (strtolower($sWrk) == "all") { // Display all records
				$nDisplayRecs = -1;
			} else {
				$nDisplayRecs = 20;  // Non-numeric, load default
			}
		}
		$_SESSION[ewSessionTblRecPerPage] = $nDisplayRecs; // Save to Session

		// Reset Start Position (Reset Command)
		$nStartRec = 1;
		$_SESSION[ewSessionTblStartRec] = $nStartRec;
	} else {
		if (@$_SESSION[ewSessionTblRecPerPage] <> "") {
			$nDisplayRecs = $_SESSION[ewSessionTblRecPerPage]; // Restore from Session
		} else {
			$nDisplayRecs = 20; // Load Default
		}
	}
}

//-------------------------------------------------------------------------------
// Function ResetSearch
// - Clear all search parameters

function ResetSearch() 
{

	// Clear search where
	$sSrchWhere = "";
	$_SESSION[ewSessionTblSearchWhere] = $sSrchWhere;

	// Clear advanced search parameters
	$_SESSION[ewSessionTblAdvSrch . "_x_status"] = "";
	$_SESSION[ewSessionTblAdvSrch . "_x_title"] = "";
	$_SESSION[ewSessionTblAdvSrch . "_x_description"] = "";
	$_SESSION[ewSessionTblAdvSrch . "_x_author_id"] = "";
	$_SESSION[ewSessionTblAdvSrch . "_x_modified_id"] = "";
	$_SESSION[ewSessionTblAdvSrch . "_x_created_date"] = "";
	$_SESSION[ewSessionTblAdvSrch . "_x_updated_date"] = "";
	$_SESSION[ewSessionTblBasicSrch] = "";
	$_SESSION[ewSessionTblBasicSrchType] = "";
}

//-------------------------------------------------------------------------------
// Function RestoreSearch
// - Restore all search parameters
//

function RestoreSearch()
{

	// Restore advanced search settings
	$GLOBALS["x_status"] = @$_SESSION[ewSessionTblAdvSrch . "_x_status"];
	$GLOBALS["x_title"] = @$_SESSION[ewSessionTblAdvSrch . "_x_title"];
	$GLOBALS["x_description"] = @$_SESSION[ewSessionTblAdvSrch . "_x_description"];
	$GLOBALS["x_author_id"] = @$_SESSION[ewSessionTblAdvSrch . "_x_author_id"];
	$GLOBALS["x_modified_id"] = @$_SESSION[ewSessionTblAdvSrch . "_x_modified_id"];
	$GLOBALS["x_created_date"] = @$_SESSION[ewSessionTblAdvSrch . "_x_created_date"];
	$GLOBALS["x_updated_date"] = @$_SESSION[ewSessionTblAdvSrch . "_x_updated_date"];
	$GLOBALS["psearch"] = @$_SESSION[ewSessionTblBasicSrch];
	$GLOBALS["psearchtype"] = @$_SESSION[ewSessionTblBasicSrchType];
}

//-------------------------------------------------------------------------------
// Function SetUpSortOrder
// - Set up Sort parameters based on Sort Links clicked
// - Variables setup: sOrderBy, Session(TblOrderBy), Session(Tbl_Field_Sort)

function SetUpSortOrder()
{
	global $sOrderBy;
	global $sDefaultOrderBy;

	// Check for Ctrl pressed
	if (strlen(@$_GET["ctrl"]) > 0) {
		$bCtrl = true;
	} else {
		$bCtrl = false;
	}

	// Check for an Order parameter
	if (strlen(@$_GET["order"]) > 0) {
		$sOrder = @$_GET["order"];

		// Field `status`
		if ($sOrder == "status") {
			$sSortField = "`status`";
			$sLastSort = @$_SESSION[ewSessionTblSort . "_x_status"];
			$sThisSort = ($sLastSort == "ASC") ? "DESC" : "ASC";
			$_SESSION[ewSessionTblSort . "_x_status"] = $sThisSort;
		} else {
			if (!($bCtrl) && @$_SESSION[ewSessionTblSort . "_x_status"] <> "") { $_SESSION[ewSessionTblSort . "_x_status"] = "" ; }
		}

		// Field `image`
		if ($sOrder == "image") {
			$sSortField = "`image`";
			$sLastSort = @$_SESSION[ewSessionTblSort . "_x_image"];
			$sThisSort = ($sLastSort == "ASC") ? "DESC" : "ASC";
			$_SESSION[ewSessionTblSort . "_x_image"] = $sThisSort;
		} else {
			if (!($bCtrl) && @$_SESSION[ewSessionTblSort . "_x_image"] <> "") { $_SESSION[ewSessionTblSort . "_x_image"] = "" ; }
		}

		// Field `title`
		if ($sOrder == "title") {
			$sSortField = "`title`";
			$sLastSort = @$_SESSION[ewSessionTblSort . "_x_title"];
			$sThisSort = ($sLastSort == "ASC") ? "DESC" : "ASC";
			$_SESSION[ewSessionTblSort . "_x_title"] = $sThisSort;
		} else {
			if (!($bCtrl) && @$_SESSION[ewSessionTblSort . "_x_title"] <> "") { $_SESSION[ewSessionTblSort . "_x_title"] = "" ; }
		}
		if ($bCtrl) {
			$sOrderBy = @$_SESSION[ewSessionTblOrderBy];
			$pos = strpos($sOrderBy, $sSortField . " " . $sLastSort);
			if ($pos === false) {
				if ($sOrderBy <> "") { $sOrderBy .= ", "; }
				$sOrderBy .= $sSortField . " " . $sThisSort;
			} else {
				$sOrderBy = str_replace($sSortField . " " . $sLastSort, $sSortField . " " . $sThisSort, $sOrderBy);
			}
			$_SESSION[ewSessionTblOrderBy] = $sOrderBy;
		} else {
			$_SESSION[ewSessionTblOrderBy] = $sSortField . " " . $sThisSort;
		}
		$_SESSION[ewSessionTblStartRec] = 1;
	}
	$sOrderBy = @$_SESSION[ewSessionTblOrderBy];
	if ($sOrderBy == "") {
		if (ewSqlOrderBy <> "" && ewSqlOrderBySessions <> "") {
			$sOrderBy = ewSqlOrderBy;
			@$_SESSION[ewSessionTblOrderBy] = $sOrderBy;
			$arOrderBy = explode(",", ewSqlOrderBySessions);
			for($i=0; $i<count($arOrderBy); $i+=2) {
				@$_SESSION[ewSessionTblSort . "_" . $arOrderBy[$i]] = $arOrderBy[$i+1];
			}
		}
	}
}

//-------------------------------------------------------------------------------
// Function SetUpStartRec
//- Set up Starting Record parameters based on Pager Navigation
// - Variables setup: nStartRec

function SetUpStartRec()
{

	// Check for a START parameter
	global $nStartRec;
	global $nDisplayRecs;
	global $nTotalRecs;
	if (strlen(@$_GET[ewTblStartRec]) > 0) {
		$nStartRec = @$_GET[ewTblStartRec];
		$_SESSION[ewSessionTblStartRec] = $nStartRec;
	} elseif (strlen(@$_GET["pageno"]) > 0) {
		$nPageNo = @$_GET["pageno"];
		if (is_numeric($nPageNo)) {
			$nStartRec = ($nPageNo-1)*$nDisplayRecs+1;
			if ($nStartRec <= 0) {
				$nStartRec = 1;
			} elseif ($nStartRec >= intval(($nTotalRecs-1)/$nDisplayRecs)*$nDisplayRecs+1) {
				$nStartRec = intval(($nTotalRecs-1)/$nDisplayRecs)*$nDisplayRecs+1;
			}
			$_SESSION[ewSessionTblStartRec] = $nStartRec;
		} else {
			$nStartRec = @$_SESSION[ewSessionTblStartRec];
		}
	} else {
		$nStartRec = @$_SESSION[ewSessionTblStartRec];
	}

	// Check if correct start record counter
	if (!(is_numeric($nStartRec)) || ($nStartRec == "")) { // Avoid invalid start record counter
		$nStartRec = 1; // Reset start record counter
		$_SESSION[ewSessionTblStartRec] = $nStartRec;
	} elseif ($nStartRec > $nTotalRecs) { // Avoid starting record > total records
		$nStartRec = intval(($nTotalRecs-1)/$nDisplayRecs)*$nDisplayRecs+1; // Point to last page first record
		$_SESSION[ewSessionTblStartRec] = $nStartRec;
	}
}

//-------------------------------------------------------------------------------
// Function ResetCmd
// - Clear list page parameters
// - RESET: reset search parameters
// - RESETALL: reset search & master/detail parameters
// - RESETSORT: reset sort parameters

function ResetCmd()
{

	// Get Reset command
	if (strlen(@$_GET["cmd"]) > 0) {
		$sCmd = @$_GET["cmd"];
		if (strtolower($sCmd) == "reset") { // Reset search criteria
			ResetSearch();
		} elseif (strtolower($sCmd) == "resetall") { // Reset search criteria and session vars
			ResetSearch();
		} elseif (strtolower($sCmd) == "resetsort") { // Reset sort criteria
			$sOrderBy = "";
			$_SESSION[ewSessionTblOrderBy] = $sOrderBy;
			if (@$_SESSION[ewSessionTblSort . "_x_status"] <> "") { $_SESSION[ewSessionTblSort . "_x_status"] = ""; }
			if (@$_SESSION[ewSessionTblSort . "_x_image"] <> "") { $_SESSION[ewSessionTblSort . "_x_image"] = ""; }
			if (@$_SESSION[ewSessionTblSort . "_x_title"] <> "") { $_SESSION[ewSessionTblSort . "_x_title"] = ""; }
		}

		// Reset start position (Reset command)
		$nStartRec = 1;
		$_SESSION[ewSessionTblStartRec] = $nStartRec;
	}
}
?>
